//
//  RootViewController.m
//  TestCallBack
//
//  Created by csdc-iMac on 14-7-17.
//  Copyright (c) 2014年 JuneWang. All rights reserved.
//

#import "RootViewController.h"
#import "SecondViewController.h"
#import "BlockObject.h"
#import "DataSource.h"
@interface RootViewController ()
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;

//A视图
//一个指向B视图的成员变量
@property (nonatomic, strong) SecondViewController *second;

@end

@implementation RootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ChangeNameNotification:) name:@"ChangeNameNotification" object:nil];
    }



- (IBAction)showSecondView:(id)sender {
    SecondViewController *second = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    second.delegate = self;
    second.flag = 0;
    [self presentViewController:second animated:YES completion:nil];
}

// 实现代理方法
-(void)showName:(NSString *)nameString{
    self.nameLabel.text = nameString;
}
-(void)ChangeNameNotification:(NSNotification*)notification{
    NSDictionary *nameDictionary = [notification userInfo];
    self.nameLabel.text = [nameDictionary objectForKey:@"name"];
}

- (IBAction)showSecondWithBlock:(id)sender {
    
    SecondViewController *secondCtr = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

    secondCtr.flag = 1;
    [self presentViewController:secondCtr animated:YES completion:nil];
    secondCtr.block = ^(NSString *str){
    self.nameLabel.text = str;
    };
}

- (IBAction)changeButtonColor:(UIButton *)sender {
    
    // 此处标注一点,利用block传值时,需要具备以下几个步骤,假如从A界面往B界面传值
    // (1)在A界面定义一个包含参数的block类型,在A界面有一个以block作为参数的方法
    // (2)在B界面调用A界面这个方法,并且在block里面写上代码,代码里面一定要重点写好如何利用block的参数(就像这里的newColor),完成传值功能
    // (3)在A界面执行block代码块
    
    [BlockObject changeButtonColor:^(UIColor*newColor){
        sender.backgroundColor = newColor;
    }];
    
    
    
    /*    
     //以下为一些简单的block测试，想看效果的话，取消注释即可
    
    int (^myblock_1)(int a, int b) = ^(int a, int b){
        return a + b;
    };
    NSLog(@"------%d", myblock_1(1, 2));
    
    //__block 变量
    __block int num;
    int (^myblock_2)(int a, int b) = ^(int a, int b){
        num = a + b;
        return num;
    };
    NSLog(@"++++++%d", myblock_2(2, 3));
    
    //Block传递函数指针
    int (^test_1)()= ^{
        NSLog(@"I");
        return 1;
    };
    int (^test_2)()= ^{
        NSLog(@"MANNA");
        return 2;
    };
    int (^test_3)()= ^{
        NSLog(@"PLAY");
        return 3;
    };
    [self testBlock:test_1];
    [self testBlock:test_2];
    [self testBlock:test_3];
    
    //多Block参数
    [self testBlock_Concurrency:^{
        NSLog(@"这是第一个Block.");
        return 100;
    }block_2:^(int p){
        NSLog(@"这是第二个Block.%d",p);
    }block_3:^{
        NSLog(@"这是第三个Block.");
    }];
    
    */
}


- (void)testBlock:(int (^)())test_block
{
    int result = test_block();
    NSLog(@"注意先后顺序，结果是:%d", result);
}

- (void)testBlock_Concurrency:(int (^)())block_1 block_2:(void(^)(int))block_2 block_3:(void(^)())block_3
{
    int result = block_1();
    block_2(100);
    block_3();
    
    NSLog(@"多么神奇的Block.result=%d.", result);
}


-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    //如果想测试通过UserDefault方式传值或者通过单例方式传值，取消以下注释即可

    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"myNameText"] length] != 0) {
        self.nameLabel.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"myNameText"];
        [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"myNameText"];
    }
    
    
    
    // 单例的办法进行传值
    DataSource *dataSource = [DataSource sharedDataSource];
    if ([dataSource.myName length] != 0) {
        self.nameLabel.text = dataSource.myName;
        // 置空,下一次填写值时候再用
        dataSource.myName = @"";
    }

}



// 注册监听变化属性的方法

- (IBAction)addSecondObserve:(id)sender {
    
    self.second = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    // 注册KVO监听self.second的userName属性的变化
    [self.second addObserver:self forKeyPath:@"userName" options:NSKeyValueObservingOptionNew context:nil];
    
    [self presentViewController:self.second animated:YES completion:nil];
    
}

// KVO实现监听
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    //此处监听key对应值的变化情况
    if ([keyPath isEqualToString:@"userName"]) {
        self.nameLabel.text = self.second.userName;
    }
}

//清理观察
- (void)dealloc
{
    // 清楚通知的观察者
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    // 清楚KVO中second的观察者
    [self.second removeObserver:self forKeyPath:@"userName"];
}






- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
